package com.clouddot.baipai.ui.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.clouddot.baipai.R;
import com.clouddot.baipai.http.entity.response.getSchoolInfoResponse;
import com.clouddot.baipai.infrastructure.environment.Env;
import com.clouddot.baipai.util.EscapeUnescapeUtil;
import com.danikula.videocache.HttpProxyCacheServer;
import com.danikula.videocache.file.FileNameGenerator;
import com.xiao.nicevideoplayer.NiceVideoPlayer;
import com.xiao.nicevideoplayer.TxVideoPlayerController;

import org.wxy.sdk.util.imageloader.ImageLoaderUtil;

import java.util.List;

/**
 * Created by wxy on 2018/8/26.
 */

public class V_SchoolsAdapter extends PagerAdapter {
    private List<getSchoolInfoResponse> dataList;
    private Context context;

    public V_SchoolsAdapter(Context context, List<getSchoolInfoResponse> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        position %= dataList.size();

        final hander hander = new hander();

        getSchoolInfoResponse response = dataList.get(position);
        if (response.BaseData.Detail != null && !response.BaseData.Detail.isEmpty()) {
            final String html = EscapeUnescapeUtil.unescape(response.BaseData.Detail);
            hander.tvOne.setBackgroundColor(0);
            hander.tvOne.getSettings().setDefaultTextEncodingName("UTF -8");
            hander.tvOne.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);
            hander.tvOne.setVisibility(View.VISIBLE);
        } else {
            hander.tvOne.setVisibility(View.GONE);
        }
        if (response.VideoData == null || response.VideoData.isEmpty() || response.VideoData.get(0).RealPathName.isEmpty()) {
            hander.VideoView.setVisibility(View.GONE);
        } else {
            hander.videoPath = response.VideoData.get(0).RealPathName;
            String url = "http://" + Env.getAppContext().IP + response.VideoData.get(0).RealPathName;
            String proxyUrl = hander.getProxy().getProxyUrl(url);
            hander.VideoView.setPlayerType(NiceVideoPlayer.TYPE_IJK);
            hander.VideoView.setUp(proxyUrl, null);
            hander.controller.setTitle(url);
            hander.VideoView.setController(hander.controller);
            hander.VideoView.setVisibility(View.VISIBLE);

        }
        if (response.ThumbnailData == null || response.ThumbnailData.isEmpty() || response.ThumbnailData.get(0).RealPathName.isEmpty()) {
            hander.imageView.setVisibility(View.GONE);
        } else {
            hander.imageView.setVisibility(View.VISIBLE);
            ImageLoaderUtil.getInstance().loadImage("http://" + Env.getAppContext().IP + response.ThumbnailData.get(0).RealPathName, hander.imageView);
        }

        hander.title.setText(response.BaseData.Title);


        ViewParent viewParent = hander.ret.getParent();
        if (viewParent != null) {
            ViewGroup parent = (ViewGroup) viewParent;
            parent.removeView(hander.ret);
        }
        container.addView(hander.ret);
        return hander.ret;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
    }


    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == (View) object;
    }

    private class hander {
        private View ret = null;
        TextView title;
        ImageView imageView;
        WebView tvOne;
        NiceVideoPlayer VideoView;
        private HttpProxyCacheServer proxy;
        private String videoPath;
        private TxVideoPlayerController controller;

        public HttpProxyCacheServer getProxy() {
            return proxy == null ? (proxy = newProxy()) : proxy;
        }

        private HttpProxyCacheServer newProxy() {
            return new HttpProxyCacheServer.Builder(context)
                    .maxCacheSize(1024 * 1024 * 1024)
                    .fileNameGenerator(new MyFileNameGenerator())
                    .build();
        }

        public class MyFileNameGenerator implements FileNameGenerator {
            public String generate(String url) {
                return "/BanPai" + videoPath;
            }
        }

        public hander() {
            ret = LayoutInflater.from(context).inflate(R.layout.v_adapter_school_info_item, null);
            title = ret.findViewById(R.id.title);
            imageView = ret.findViewById(R.id.imageOne);
            tvOne = ret.findViewById(R.id.tvOne);
            VideoView = ret.findViewById(R.id.VideoView);
            controller = new TxVideoPlayerController(context);

        }
    }


}